package com.cdn.allmobilestore.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cdn.allmobilestore.R;
import com.cdn.allmobilestore.adapter.FragmentPagerAdapter;
import com.cdn.allmobilestore.adapter.HomeAdapter;
import com.cdn.allmobilestore.ui.BaseFragment;
import com.cdn.allmobilestore.ui.CategoryFragment;
import com.cdn.allmobilestore.ui.HomeFragment;
import com.cdn.allmobilestore.ui.ListAppFragment;
import com.cdn.allmobilestore.ulti.Constant;
import com.cdn.allmobilestore.widget.TabIndicator;

public class CategoryTabActivity extends BaseActivity implements FragmentPagerAdapter.TabChangedListener, View.OnClickListener,OnPageChangeListener  {
	
	private ViewPager mPager;
	private String mCurrentTag = Constant.FRAGMENT_TAG_HOT;
	private View mTabContainerBg;
	private TabIndicator tab_hot,tab_topapp,tab_category;
	BaseFragment hotFragment,topappFragment,menuFragment;
	BaseFragment categoryFragment;
	
    private FragmentPagerAdapter mPagerAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setCustomContentView(R.layout.activity_category_tab);
		
		Intent intent = getIntent();		
		String categoryId = intent.getStringExtra(Constant.INTENT_EXTRA_CATEGORY_PARENT_ID);
		String categoryname = intent.getStringExtra(Constant.INTENT_EXTRA_CATEGORY_PARENT_NAME);		
		setBarTitle(categoryname);
		setHomeButtonEnable(true);
		mTabContainerBg = findViewById(R.id.tab_container_bg);
		this.tab_hot = ((TabIndicator)findViewById(R.id.tab_indicator_hot));
	    this.tab_topapp = ((TabIndicator)findViewById(R.id.tab_indicator_topapp));
	    this.tab_category = ((TabIndicator)findViewById(R.id.tab_indicator_category));
	   
		mPager = (ViewPager) findViewById(R.id.tab_pager);			
		FragmentManager localFragmentManager = getSupportFragmentManager();
	    FragmentTransaction localFragmentTransaction = localFragmentManager.beginTransaction();	    
	    hotFragment = (BaseFragment)localFragmentManager.findFragmentByTag(Constant.FRAGMENT_TAG_HOT);
	    if(hotFragment == null)
	    {
	    	hotFragment = new ListAppFragment();
	    	((ListAppFragment) hotFragment).setCategory(categoryId);
	    	((ListAppFragment) hotFragment).setListAppType(Constant.LISTAPP_TYPE_HOT);
	    	localFragmentTransaction.add(R.id.tab_pager,hotFragment,Constant.FRAGMENT_TAG_HOT);
	    }
	    	
	    topappFragment = (BaseFragment)localFragmentManager.findFragmentByTag(Constant.FRAGMENT_TAG_DOWNLOAD);
	    if(topappFragment == null)
	    {
	    	topappFragment = new ListAppFragment();
	    	((ListAppFragment) topappFragment).setCategory(categoryId);
	    	localFragmentTransaction.add(R.id.tab_pager,topappFragment,Constant.FRAGMENT_TAG_DOWNLOAD);
	    }
	    
	    categoryFragment = (BaseFragment)localFragmentManager.findFragmentByTag(Constant.FRAGMENT_TAG_CATEGORY);
	    if(categoryFragment == null)
	    {
	    	categoryFragment = new CategoryFragment();
	    	((CategoryFragment) categoryFragment).setCategory(categoryId);
	    	localFragmentTransaction.add(R.id.tab_pager,categoryFragment,Constant.FRAGMENT_TAG_CATEGORY);
	    }
	    
	    
	    
	    mPager.setOnPageChangeListener(this);
	    mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager());
	    mPagerAdapter.setTabChangedListener(this);
		mPagerAdapter.addFragment(tab_hot, hotFragment);		
		mPagerAdapter.addFragment(tab_topapp,topappFragment);
		mPagerAdapter.addFragment(tab_category, categoryFragment);
		mPagerAdapter.setContext(this);
		
		
		if (!localFragmentTransaction.isEmpty())
	     {
	        localFragmentTransaction.commitAllowingStateLoss();
	        getSupportFragmentManager().executePendingTransactions();
	     }
		int i = mPagerAdapter.getPositionByTag(this.mCurrentTag);
	    this.mPagerAdapter.selectTab(i);
	    mPager.setCurrentItem(i);
	    mPager.setAdapter(mPagerAdapter);
	   
	}
	
	public class ScreenSlidePageFragment extends BaseFragment {
		String txt;
		ScreenSlidePageFragment(String text){
			this.txt = text;
		}
	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	            Bundle savedInstanceState) {
	        TextView textview = new TextView(container.getContext());
	        textview.setText(this.txt);
	        if(this.txt.equals("home"))
	        	textview.setBackgroundColor(Color.RED);
	        else if(this.txt.equals("Lottery"))
	        	textview.setBackgroundColor(Color.WHITE);
	        else if(this.txt.equals("Category"))
	        	textview.setBackgroundColor(Color.BLUE);
	        else
	        	textview.setBackgroundColor(Color.YELLOW);
	        //txt.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
	        return textview;
	    	
	    
	    }
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public static void setCurrentTag(CategoryTabActivity activity, String tag)
	{
		activity.mCurrentTag = tag;
	}
	
	
	@Override
	 protected void onRestoreInstanceState(Bundle paramBundle)
	  {
	    super.onRestoreInstanceState(paramBundle);
	    RelativeLayout.LayoutParams localLayoutParams = (RelativeLayout.LayoutParams)this.mTabContainerBg.getLayoutParams();
	    localLayoutParams.leftMargin = paramBundle.getInt("tabLeftMargin");
	    localLayoutParams.rightMargin = paramBundle.getInt("tabRightMargin");
	    
	    this.mTabContainerBg.setLayoutParams(localLayoutParams);
	  }
   
	@Override
	  protected void onSaveInstanceState(Bundle paramBundle)
	  {
	    RelativeLayout.LayoutParams localLayoutParams = (RelativeLayout.LayoutParams)this.mTabContainerBg.getLayoutParams();
	    paramBundle.putInt("tabLeftMargin", localLayoutParams.leftMargin);
	    paramBundle.putInt("tabRightMargin", localLayoutParams.rightMargin);
	    super.onSaveInstanceState(paramBundle);
	  }
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onTabChanged(int position) {
		// TODO Auto-generated method stub
		this.mPager.setCurrentItem(position);
	}

	//public void onPageScrolled(int paramInt1, float paramFloat, int paramInt2)
		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			 
			 RelativeLayout.LayoutParams localLayoutParams = (RelativeLayout.LayoutParams)CategoryTabActivity.this.mTabContainerBg.getLayoutParams();
			 float leftMargin1 = CategoryTabActivity.this.getResources().getDimensionPixelSize(R.dimen.tab_container_bg_leftmargin);
			 TabIndicator tab = CategoryTabActivity.this.mPagerAdapter.getTab(position);
			 if(tab != null)
			 {
				 float leftMargin2 = positionOffset * tab.getWidth();
				 float leftMargin3 = leftMargin1 + leftMargin2;
				 float rightMargin = 0.0F - leftMargin2;
				 for(int i =1; i <= position;i++)
				 {
					 leftMargin3 += tab.getWidth();
					 rightMargin -= tab.getWidth();
				 }
				 localLayoutParams.leftMargin = (int)leftMargin3;
				 localLayoutParams.rightMargin = (int)rightMargin;
				 CategoryTabActivity.this.mTabContainerBg.setLayoutParams(localLayoutParams);	    
			 }
			 else
				 CategoryTabActivity.this.mPager.setCurrentItem(position);
			 
			 
		}

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			CategoryTabActivity.this.mPagerAdapter.selectTab(position);
			CategoryTabActivity.setCurrentTag(CategoryTabActivity.this, CategoryTabActivity.this.mPagerAdapter.getTagByPosition(position));
			setRequestedOrientation(2);
		}


		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}
}
